﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cole.Entities;
using Cole.Service;

namespace Cole.GUI
{
    public partial class frmListDocentes : Form
    {
        private IDocenteService docenteService;
        public frmListDocentes()
        {
            InitializeComponent();
            docenteService = new DocenteService();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var criterio = txtCriterio.Text == string.Empty ? "%" : txtCriterio.Text;

            docenteBindingSource.DataSource = docenteService.GetFromDocentesByCriterio(criterio);
        }

        private void frmListDocentes_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmDocente = new frmDocentes();
            frmDocente.NuevoDocente();
            frmDocente.ShowDialog();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var docente = docenteBindingSource.Current as Docente;

            if (docente!=null)
            {
                var frmDocente = new frmDocentes();
                frmDocente.EditarDocente(docente.Id);
                frmDocente.ShowDialog();
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var docente = docenteBindingSource.Current as Docente;

            if (docente!=null)
            {

                var rpt = MessageBox.Show(this, string.Format("¿Desea eliminar el Docente: {0}?", 
                    docente.Nombres), "Mensaje",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (rpt == DialogResult.Yes)
                {
                    docenteService.RemoveDocente(docente.Id);
                    docenteBindingSource.DataSource
                        = docenteService.GetAllFromDocentes();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Docente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
